﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BLL;
using Model;
using MyStudentProj;

namespace ProjectStudent
{
    public partial class ScoresManage : Form
    {
        public ScoresManage()
        {
            InitializeComponent();
            dataGridView1.AutoGenerateColumns = false;
            getScorePage();
        }
        ScoreBLL scoreBLL = new ScoreBLL();
       
        public void getScorePage()
        {
            int total = scoreBLL.getScoreCount("");
            lb_total.Text = total.ToString();
            int pageSize = Convert.ToInt32(comboBox1.Text);
            int pageTotal = (int)Math.Ceiling((double)total / pageSize);
            lb_pageCount.Text = pageTotal.ToString();
            int pageNumber = Convert.ToInt32(txtCurrentPage.Text);
            List<Score> scores = scoreBLL.getPageScore(pageNumber, pageSize, "");
            dataGridView1.DataSource = scores;
        }

        private void btnHome_Click(object sender, EventArgs e)
        {
            txtCurrentPage.Text = "1";
            getScorePage();
        }

        private void btnPre_Click(object sender, EventArgs e)
        {
            int pageNumber = Convert.ToInt32(txtCurrentPage.Text);
            if (pageNumber > 1)
            {
                pageNumber--;
                txtCurrentPage.Text = pageNumber.ToString();
            }
            else
            {
                MessageBox.Show("当前已经是第一页！");
            }
            getScorePage();
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            int pageNumber = Convert.ToInt32(txtCurrentPage.Text);
            int pageTotal = Convert.ToInt32(lb_pageCount.Text);
            if (pageNumber < pageTotal)
            {
                pageNumber++;
                txtCurrentPage.Text = pageNumber.ToString();
            }
            else
            {
                MessageBox.Show("当前已经是最后一页！");
            }
            getScorePage();
        }

        private void btnEnd_Click(object sender, EventArgs e)
        {
            txtCurrentPage.Text = lb_pageCount.Text;
            getScorePage();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            string keyword = txtSearch.Text;
            if (string.IsNullOrWhiteSpace(keyword))
            {
                txtCurrentPage.Text = "1";
                getScorePage();
                return;
            }
            List<Score> scores = scoreBLL.SearchHaveKeyWord(keyword); 
            dataGridView1.DataSource = scores;
            dataGridView1.Refresh(); 
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtCurrentPage.Text = "1";
            getScorePage();
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            txtSearch.Text = "";

        }

       

        private void btnAdd_Click(object sender, EventArgs e)
        {
            ScoreForm scoreForm = new ScoreForm(null);
            if(scoreForm.ShowDialog() == DialogResult.OK)
            {
                if(scoreBLL.AddScore(Convert.ToInt32(scoreForm.StudentID), Convert.ToInt32(scoreForm.CourseID), Convert.ToInt32(scoreForm.ScoreValue),Convert.ToDateTime(scoreForm.ExamDate) ,scoreForm.ScoreType, Convert.ToDateTime(scoreForm.CreateDate)))
                {
                    MessageBox.Show("新增成功！");
                    getScorePage();
                }
                else
                {
                    MessageBox.Show("新增失败！");
                }
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            Score score =  dataGridView1.CurrentRow.DataBoundItem as Score ;
            ScoreForm scoreForm = new ScoreForm(score);
            if (scoreForm.ShowDialog() == DialogResult.OK)
            {
                if(scoreBLL.UpdateScore(Convert.ToInt32(scoreForm.ScoreID) ,Convert.ToInt32(scoreForm.StudentID), Convert.ToInt32(scoreForm.CourseID), Convert.ToInt32(scoreForm.ScoreValue), Convert.ToDateTime(scoreForm.ExamDate), scoreForm.ScoreType, Convert.ToDateTime(scoreForm.CreateDate)))
                {
                    MessageBox.Show("编辑成功！");
                    getScorePage();
                }
                else
                {
                    MessageBox.Show("编辑失败！");
                }
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (dataGridView1.CurrentRow == null)
            {
                MessageBox.Show("请先选择要删除的数据！");
                return;
            }
            Score score = dataGridView1.CurrentRow.DataBoundItem as Score;
            //score.CourseID;
            if(MessageBox.Show("确认要删除该用户吗？", "确认删除",MessageBoxButtons.YesNo)==DialogResult.Yes)
            {
                if(scoreBLL.DeleteScore(score.ScoreID))
                {
                    MessageBox.Show("删除成功！");
                    getScorePage();
                }
                else
                {
                    MessageBox.Show("删除失败！");
                }
            }
        }

        private void btnExport_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Filter = "CSV文件 (*.csv)|*.csv";
            saveFileDialog.Title = "请选择导出CSV文件保存位置";
            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                string filePath = saveFileDialog.FileName;
                if (scoreBLL.ExportScoreToCsv(filePath))
                {
                    MessageBox.Show("导出成功！");
                }
                else
                {
                    MessageBox.Show("导出失败！");
                }
            }
        }

        private void btnImport_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "CSV文件 (*.csv)|*.csv";
            openFileDialog.Title = "请选择要导入的CSV文件";
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                string filePath = openFileDialog.FileName;
                if (scoreBLL.ImportScoreFromCsv(filePath))
                {
                    MessageBox.Show("导入成功！");
                    getScorePage();
                }
                else
                {
                    MessageBox.Show("导入失败！");
                }
            }
        }
    }
}
